package com.thinkgem.jeesite.modules.oa.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.oa.entity.Element;
import com.thinkgem.jeesite.modules.oa.entity.Firm;
import com.thinkgem.jeesite.modules.oa.entity.OaFile;
import com.thinkgem.jeesite.modules.oa.entity.SerialNum;
import com.thinkgem.jeesite.modules.oa.service.OaFileNumService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

/**
 * 生成编号，受控文件编号
 * 
 * @author maxiao
 *
 */
@Controller
@RequestMapping(value = "${adminPath}/oa/fileNum")
public class OaFileNumController extends BaseController {
	@Autowired
	private OaFileNumService oaFileNumService;
	@Autowired
	private SystemService systemService;

	/**
	 * 查询
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:oaFile:view")
	@RequestMapping(value = "list")
	public String list(OaFile oaFile, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<OaFile> page = oaFileNumService.findPage(new Page<OaFile>(request,
				response), oaFile);
		model.addAttribute("page", page);
		return "modules/oa/oaFileNumList";

	}

	// @RequestMapping(value = "list")
	// public String list(User user, HttpServletRequest request,
	// HttpServletResponse response, Model model) {
	// Page<User> page = systemService.findUser(new Page<User>(request,
	// response), user);
	// model.addAttribute("page", page);
	// return "modules/oa/oaFileNumList";
	// }
	/**
	 * 跳转页面
	 * 
	 * @param user
	 * @param model
	 * @return
	 */
	@RequiresPermissions("oa:oaFile:view")
	@RequestMapping(value = "form")
	public String form() {

		return "modules/oa/oaFileNumForm";
	}

	/**
	 * 跳转页面
	 * 
	 * @param
	 * @param
	 * @return
	 */
	@RequiresPermissions("oa:oaFileRepair:edit")
	@RequestMapping(value = "repair")
	public String repair() {

		return "modules/oa/oaRepairCode";
	}

	/**
	 * 保存表单
	 * 
	 * @param
	 * @param
	 * @param
	 * @return
	 */
	@RequiresPermissions("oa:oaFile:edit")
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public ModelAndView save(OaFile oaFile) {
		// 查询数据库中最新的一条信息 放入oaFile对象中
		OaFile oaFileOld = new OaFile();
		oaFileOld = oaFileNumService.findLastOaFile();
		// 如果为空，给予初始流水号
		if (oaFileOld == null) {
			oaFile.setNum(1);
		} else {
			// 如果不为空，流水号+1 ，赋给oaFile
			oaFile.setNum(oaFileOld.getNum() + 1);
		}

		ModelAndView mv = new ModelAndView("modules/oa/oaFileNumForm");
		// 判断前台传来数据是否为空
		if (oaFile.getDeptCode().equals("") || oaFile.getFileType().equals("")) {
			mv.addObject("massage", "对不起，请选择部门或文件类型");
			return mv;
		} else {
			// 保存数据库
			oaFileNumService.savaOaFile(oaFile);
			// 将生成的流水号传递给前台，显示流水号
			mv.addObject("code", oaFile);
			return mv;
		}
	}

	/**
	 * 生成维修板件编号
	 * 
	 */
	@RequiresPermissions("oa:oaFileRepair:edit")
	@SuppressWarnings("null")
	@RequestMapping(value = "createRepairCode", method = RequestMethod.POST)
	public ModelAndView createRepairCode(String firmCode, String elementCode) {
		ModelAndView mv = new ModelAndView("modules/oa/oaRepairCode");
		// 判断前台传来数据是否为空
		if (firmCode.equals("") || elementCode.equals("")) {
			mv.addObject("massage", "对不起，请选择厂商名或者板材型号");
			return mv;
		} else {

			// 根据厂商名称代号 查询数据库中原有的流水号
			SerialNum serialNum = new SerialNum();
			SerialNum serialNumOld = oaFileNumService.findLastSerialNum(firmCode);
			// 如果为空，给予初始流水号
			if (serialNumOld == null) {
				serialNum.setSerialCode(1);
			} else {
				// 如果不为空，流水号+1 ，赋给serialNum
				serialNum.setSerialCode(serialNumOld.getSerialCode() + 1);
			}
			serialNum.setElementCode(elementCode);
			serialNum.setFirmCode(firmCode);
			// 保存数据库
			oaFileNumService.savaSerialNum(serialNum);
			// 将生成的流水号传递给前台，显示流水号
			mv.addObject("code", serialNum);
			return mv;
		}

	}

	@ResponseBody
	@RequestMapping("findElementNames")
	public List<Element> findElementNames() {
		return oaFileNumService.findElementNames();
	}

	@ResponseBody
	@RequestMapping("findFirmNames")
	public List<Firm> findFirmNames() {
		return oaFileNumService.findFirmNames();
	}
}
